package com.ellricky.baselib.ui.overdata

import android.view.View
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.SummaryMonth
import com.ellricky.baselib.entity.new.TeacherSummaryData
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.widget.DataLayout
import com.ellricky.baselib.widget.TYPE_MONTH
import com.ellricky.baselib.widget.TYPE_YEAR
import kotlinx.android.synthetic.main.base_fragment_teacher_data.*
import java.util.*

class OverviewConsultantDataFragment(val id: String = "") : BaseFragment<Void, BasePresenter<Void>>() {
    private var year: String = ""
    private var month: String = ""
    override fun getContentLayout(): Int {
        return R.layout.base_fragment_consultant_data
    }

    override fun initUIView(p0: View?) {
        val currentMonth = getCurrentMonth(System.currentTimeMillis())
        val currentYear = getCurrentYear(System.currentTimeMillis())
        summaryMonthData.setShowType(TYPE_MONTH)
        summaryYearData.setShowType(TYPE_YEAR)
        summaryMonthData.setMonthData(System.currentTimeMillis())
        summaryYearData.setMonthData(System.currentTimeMillis())

        summaryMonthData.setOnDataLayoutListener(object : DataLayout.OnDataLayoutListener {
            override fun onLeftClick() {
                val currentTime = summaryMonthData.getCurrentTime()
                val month = getCurrentMonth(currentTime)
                loadData(month, year)
            }

            override fun onRightClick() {
                val currentTime = summaryMonthData.getCurrentTime()
                val month = getCurrentMonth(currentTime)
                loadData(month, year)
            }
        })

        summaryYearData.setOnDataLayoutListener(object : DataLayout.OnDataLayoutListener {
            override fun onLeftClick() {
                val currentTime = summaryYearData.getCurrentTime()
                val year = getCurrentYear(currentTime)
                loadData(month, year)
            }

            override fun onRightClick() {
                val currentTime = summaryYearData.getCurrentTime()
                val year = getCurrentYear(currentTime)
                loadData(month, year)
            }
        })
        loadData(currentMonth, currentYear)
    }

    private fun loadData(month: String, year: String) {
        this.year = year
        this.month = month
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "saleDataList",
            "eid" to LocalCacheUtil.getUid(),
            "year_data" to year,
            "month_data" to month,
            "emp_id" to if (id.isEmpty()) LocalCacheUtil.getUid() else id
        )
        if (LocalCacheUtil.getCardType() == BaseConstant.CARD_PRESIDENT) {
            paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        }
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<TeacherSummaryData>(mContext) {
                override fun onSuccess(data: TeacherSummaryData, msg: String) {
                    summaryData.setFirstData(data.total_return_number.toString())
                    summaryData.setSecondData(data.total_trial_course_num.toString())
                    summaryData.setThirdData(data.total_apply_num.toString())
                    summaryData.setFourData(data.total_amount.toString())
                    summaryData.setFiveData(data.course_number_ratio.toString())

                    val dataMonth = data.monthData
                    setMonthData(dataMonth)

                    val dataYear = data.yearData
                    summaryYearData.setFirstData(dataYear.year_return_number.toString())
                    summaryYearData.setSecondData(dataYear.year_trial_course_num.toString())
                    summaryYearData.setThirdData(dataYear.year_apply_num.toString())
                    summaryYearData.setFourData(dataYear.year_amount.toString())
                    summaryYearData.setFiveData(dataYear.course_number_ratio.toString())
                }

            })
    }

    private fun setMonthData(dataMonth: SummaryMonth) {
        summaryMonthData.setFirstData(dataMonth.month_return_number.toString())
        summaryMonthData.setSecondData(dataMonth.month_trial_course_num.toString())
        summaryMonthData.setThirdData(dataMonth.month_apply_num.toString())
        summaryMonthData.setFourData(dataMonth.month_amount.toString())
        summaryMonthData.setFiveData(dataMonth.course_number_ratio.toString())
    }

    private fun getCurrentYear(time: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = time
        return cal[Calendar.YEAR].toString()
    }

    private fun getCurrentMonth(time: Long): String {
        val cal = Calendar.getInstance()
        cal.timeInMillis = time
        val month = cal[Calendar.MONTH] + 1
        return month.toString()
    }
}